//
//  WJEssAndNewPLVC.m
//  WJBSApp
//
//  Created by dllo on 16/7/22.
//  Copyright © 2016年 王景伟. All rights reserved.
//
// 上拉加载和下拉刷新
// 抓个评论的接口-->id是主接口里面的id
// http://api.budejie.com/api/api_open.php?&a=dataList&c=comment&page=1&data_id=19446453

#import "WJEssAndNewPLVC.h"
#import "WJFowardVC.h"
#import "WJMainTableViewCell.h"
#import "WJPLCell.h"
#import "WJPLmodel.h"
#import "WJLastViewController.h" // 关注界面
#import "WJ_TJDY_VC.h"

@interface WJEssAndNewPLVC ()<UITableViewDataSource,UITableViewDelegate,WJMainTableViewCellDelegate>
@property (nonatomic, strong) WJBaseTableView *tableView;
@property (nonatomic, strong) NSMutableArray *array;
@property (nonatomic, assign) NSInteger pageId;
@end

@implementation WJEssAndNewPLVC
static NSString *const PLEssCell = @"PLEssCell";
static NSString *const PLCell = @"PLCell";
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.array = [NSMutableArray array];

    [self config];

    [self createView];
    
    [self createDataWithIsDown:YES];
}

- (void)config {
    self.view.backgroundColor = BSWhiteColor;
    self.navigationItem.title = @"评论";
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithImage:@"comment_nav_item_share_icon" hightImage:@"comment_nav_item_share_icon_click" target:self action:@selector(rightAction)];
}

#pragma mark - 点击右面按钮进入分享界面
- (void)rightAction {
    WJFowardVC *vc = [[WJFowardVC alloc]init];
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.6];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)createView {
    self.tableView = [[WJBaseTableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[WJMainTableViewCell class] forCellReuseIdentifier:@"WJMainTableViewCell"];
    [self.tableView registerClass:[WJMainImageCell class] forCellReuseIdentifier:@"WJMainImageCell"];
    [self.tableView registerClass:[WJMainVideoCell class] forCellReuseIdentifier:@"WJMainVideoCell"];
    [self.tableView registerClass:[WJMianAudioCell class] forCellReuseIdentifier:@"WJMianAudioCell"];
    [self.tableView registerClass:[WJPLCell class] forCellReuseIdentifier:PLCell];
    [self.view addSubview:self.tableView];

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self createDataWithIsDown:YES];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self createDataWithIsDown:NO];
    }];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) return 1;
    return self.array.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section == 0)return nil;
    return @"最新评论";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) return self.layout.cellHeight + 10;
    WJPLmodel *model = self.array[indexPath.row];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:16]};
    CGRect rect = [model.content boundingRectWithSize:CGSizeMake( WIDTH - 20, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return (HEIGHT/9 - 20)/2 + 10 +  rect.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 ) {
        if (_layout.style == WJMianLayoutImage || _layout.style == WJMianLayoutGif || _layout.style == WJMianLayoutHtml) {
            WJMainImageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMainImageCell" forIndexPath:indexPath];
            cell.layout = _layout;
            cell.cellDelegate = self;
            return cell;
        } else if (_layout.style == WJMianLayoutVideo) {
            WJMainVideoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMainVideoCell" forIndexPath:indexPath];
            cell.layout = _layout;
            cell.cellDelegate = self;
            return cell;
        } else if (_layout.style == WJMianLayoutAudio) {
            WJMianAudioCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMianAudioCell" forIndexPath:indexPath];
            cell.layout = _layout;
            cell.cellDelegate = self;
            return cell;
        } else {
            WJMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WJMainTableViewCell" forIndexPath:indexPath];
            cell.layout = _layout;
            cell.cellDelegate = self;
            return cell;
        }
    } else {
        WJPLCell *cell = [tableView dequeueReusableCellWithIdentifier:PLCell];
        WJPLmodel *model = self.array[indexPath.row];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        __block typeof(self) weakSelf = self;
        cell.followIDBlock = ^(NSString *userID,NSString *userName){
            WJLastViewController *vc = [[WJLastViewController alloc]init];
            vc.userID = userID;
            vc.userName = userName;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
        cell.model = model;
        return cell;
    }
}

- (void)mainTableViewCellClickHeaderWithLayout:(WJMianLayout *)layout {
    WJLastViewController *vc = [[WJLastViewController alloc]init];
    vc.userID = layout.model.user.uid;
    vc.userName = layout.model.user.name;
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)mainTableViewCellClickFowardWithLayout:(WJMianLayout *)layout {
    WJFowardVC *vc = WJFowardVC.new;
    vc.view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.6];
    vc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
    [BSRootViewController presentViewController:vc animated:YES completion:nil];
}
- (void)mainTableViewCellClickTagWithModel:(Tag *)model {
    WJ_TJDY_VC *vc = WJ_TJDY_VC.new;
    vc.theme_id = model.idStr;
    vc.title = model.name;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)createDataWithIsDown:(BOOL)isDown {
    if (isDown) self.pageId = 1;
    
    NSString *url = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?&a=dataList&c=comment&page=%ld&data_id=%@",(long)self.pageId,self.layout.model.idStr];
    [WJNetTool GET:url body:nil header:nil response:WJJSON success:^(id resuposeObject) {
         self.pageId++;
        
        NSDictionary *dic = resuposeObject;
        
        NSArray *array = dic[@"data"];
        
        if (isDown) [self.array removeAllObjects];
        
        for (NSDictionary *dic in array) {
            WJPLmodel *model = [[WJPLmodel alloc]initWithDic:dic];
            model.userModel = [[WJPLUserModel alloc]initWithDic:model.user];
            [self.array addObject:model];
        }
        
        [self.tableView reloadData];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        if (!error) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        [self.tableView.mj_header endRefreshing];
    }];
}
@end
